package com.muyu.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.muyu.product.domain.model.AttributeGroupSaveModel;
import com.muyu.product.domain.resp.AttributeGroupPageResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.req.AttributeGroupQueryReq;
import com.muyu.product.domain.req.AttributeGroupSaveReq;
import com.muyu.product.domain.req.AttributeGroupEditReq;
import com.muyu.product.service.AttributeGroupService;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 属性组Controller
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Api(tags = "属性组")
@RestController
@RequestMapping("/attributeGroup")
public class AttributeGroupController extends BaseController {
    @Autowired
    private AttributeGroupService attributeGroupService;

    /**
     * 查询属性组列表
     */
    @ApiOperation("获取属性组列表")
    @RequiresPermissions("product:attributeGroup:list")
    @GetMapping("/list")
    public Result<TableDataInfo<AttributeGroupPageResp>> list(AttributeGroupQueryReq attributeGroupQueryReq) {
        startPage();
        TableDataInfo<AttributeGroupPageResp> tableDataInfo =
                attributeGroupService.page(AttributeGroup.queryBuild(attributeGroupQueryReq));
        return Result.success(tableDataInfo);
    }

    /**
     * 导出属性组列表
     */
    @ApiOperation("导出属性组列表")
    @RequiresPermissions("product:attributeGroup:export")
    @Log(title = "属性组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttributeGroup attributeGroup) {
        List<AttributeGroup> list = attributeGroupService.list(attributeGroup);
        ExcelUtil<AttributeGroup> util = new ExcelUtil<AttributeGroup>(AttributeGroup.class);
        util.exportExcel(response, list, "属性组数据");
    }

    /**
     * 获取属性组详细信息
     */
    @ApiOperation("获取属性组详细信息")
    @RequiresPermissions("product:attributeGroup:query")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<AttributeGroup> getInfo(@PathVariable("id") Long id) {
        return Result.success(attributeGroupService.getById(id));
    }

    /**
     * 新增属性组
     */
    @RequiresPermissions("product:attributeGroup:add")
    @Log(title = "属性组", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增属性组")
    public Result<String> add(@RequestBody AttributeGroupSaveReq attributeGroupSaveReq) {
        return toAjax(
                attributeGroupService.save(AttributeGroupSaveModel.saveReqBuild(attributeGroupSaveReq))
        );
    }

    /**
     * 修改属性组
     */
    @RequiresPermissions("product:attributeGroup:edit")
    @Log(title = "属性组", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改属性组")
    public Result<String> edit(@PathVariable Long id, @RequestBody AttributeGroupEditReq attributeGroupEditReq) {
        return toAjax(attributeGroupService.updateById(AttributeGroup.editBuild(id,attributeGroupEditReq)));
    }

    /**
     * 删除属性组
     */
    @RequiresPermissions("product:attributeGroup:remove")
    @Log(title = "属性组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除属性组")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
    public Result<String> remove(@PathVariable List<Long> ids) {
        return toAjax(attributeGroupService.removeBatchByIds(ids));
    }
}
